﻿<%@ WebHandler Language="C#" Class="SaveData" %>

using System;
using System.Web;
using System.Data;
using System.Data.OracleClient;

public class SaveData : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
		string dataToParse = context.Request.Params["command"];

		string[] items = dataToParse.Split(',');
		decimal enrollmentId = decimal.Parse(items[1]);
		DateTime start = DateTime.Parse(items[2]);
		DataLayer dal = new DataLayer();
		string message;
		System.Data.DataTable codes = dal.GetCodes(PAConfiguration.ConnectionString, dal.GetCurrent(PAConfiguration.ConnectionString, out message).TermId, out message);
		PALogManager.MessageLog(message);
		string[] comments = items[3].Split(new string[] { "###" }, StringSplitOptions.None);

		string scodes = items[0];
		string code = string.Empty;
		if (scodes.Length > 0)
		{
			code = scodes.Substring(0, 1);
			scodes = scodes.Substring(1, scodes.Length - 1);
		}
		
			decimal attCodeId = 0;

			foreach(System.Data.DataRow dr in codes.Rows)
			{
				if(!dr.IsNull("ATT_CODE")&&((string)dr["ATT_CODE"] == code))
				{
					attCodeId = (decimal)dr["ATTENDANCE_CODE_ID"];
					continue;
				}
			}

			if (attCodeId == 0)
				return;

			using (OracleConnection connection = new OracleConnection(PAConfiguration.ConnectionString))
			{
				connection.Open();
				OracleTransaction trans = connection.BeginTransaction();

				try
				{
					dal.SetAttendance(connection, trans, enrollmentId, start, attCodeId, comments[0], scodes, out message);
					PALogManager.MessageLog(PAConfiguration.ConnectionString);
					trans.Commit();
				}
				catch (System.Exception ex)
				{
					trans.Rollback();
					throw ex;
				}
			}
		
		
			context.Response.ContentType = "text/xml";
			context.Response.Write("<status>Ok</status>");
		}
	
		
  
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    }
   